package com.supreme.aspectserver.controller;

import com.alibaba.fastjson2.JSONObject;
import com.supreme.aspectserver.params.SimpleArg;
import com.supreme.aspectserver.service.RecordService;
import com.supreme.aspectserver.service.TestService;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.commonboot.utils.ReturnEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "test")
public class TestController {

    @Resource
    private TestService testService;

    @Resource
    private RecordService recordService;

    @GetMapping(value = "hello")
    public AjaxResp hello() {
        return AjaxResp.instance("hello");
    }

    @PostMapping(value = "world")
    public AjaxResp world(@RequestBody JSONObject requestBody) {
        return AjaxResp.instance(requestBody);
    }

    @GetMapping(value = "testSwitch/{type}")
    public AjaxResp testSwitch(@PathVariable(value = "type") int type) {
        final String result = testService.testForSwitch(type);
        System.out.println(result);
        return AjaxResp.instance(result);
    }

    @GetMapping(value = "testAnnoType")
    public AjaxResp testAnnoType() {
        testService.testAnnoType();
        return AjaxResp.instance();
    }

    @GetMapping(value = "testAnnoMethod")
    public AjaxResp testAnnoMethod() {
        ReturnEntity<String> result = testService.testAnnoMethod();
        return AjaxResp.instance(result);
    }

    @GetMapping(value = "testAnnoMethodThrow/{value}")
    public AjaxResp testAnnoMethodThrow(@PathVariable(value = "value") Integer value) {
//        ReturnEntity<String> result = testService.testAnnoMethodThrow(value);
        ReturnEntity<String> result = null;
        try {
            result = testService.testAnnoMethodThrow(value);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResp.instance(result);
    }

    @GetMapping(value = "testAnnoMethodAsync")
    public AjaxResp testAnnoMethodThrow() {
        ReturnEntity<String> result = testService.testAnnoMethodAsync();
        System.out.println("000 Thread.currentThread().getName() = " + Thread.currentThread().getName());
        return AjaxResp.instance(result);
    }

    @GetMapping(value = "testAnnoParameter")
    public AjaxResp testAnnoParameter() {
        testService.testAnnoParameter(new SimpleArg());
        return AjaxResp.instance();
    }

    @GetMapping(value = "testDoubleAnnoMethod")
    public AjaxResp testDoubleAnnoMethod() {
        testService.testDoubleAnnoMethod();
        return AjaxResp.instance();
    }

    @GetMapping(value = "createRecorder/{message}")
    public AjaxResp createRecorder(@PathVariable(value = "message") String message) {
        recordService.createRecord(message);
        return AjaxResp.instance();
    }

    @GetMapping(value = "updateRecorder/{message}")
    public AjaxResp updateRecord(@PathVariable(value = "message") String message) {
        recordService.updateRecord(message);
        return AjaxResp.instance();
    }

    @GetMapping(value = "createRecordCall/{message}")
    public AjaxResp createRecordCall(@PathVariable(value = "message") String message) {
        recordService.createRecordCall(message);
        return AjaxResp.instance();
    }
}
